﻿using EntityModel.Helper;
using EntityModel.PageListInfor;
using EntityModel.Product;
using IServices.ICurrency;
using IServices.IProductLend;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Services.ProductLend
{
    public class ProductLend : IProductLend
    {
        private readonly ICurrencyServer _currencyServer;
        private readonly RoutineDbContext _dbContext;

        public ProductLend(ICurrencyServer currencyServer, RoutineDbContext dbContext)
        {
            _currencyServer = currencyServer ?? throw new ArgumentNullException(nameof(currencyServer));
            _dbContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext));
        }

        /// <summary>
        /// 添加供货商
        /// </summary>
        /// <param name="_ProductLend">供货商数据集</param>
        /// <returns></returns>
        public async Task<bool> AddProductLendAync(AikeApplet_ProductLend _ProductLend)
        {
            if (_ProductLend == null) throw new ArgumentNullException(nameof(_ProductLend));
            var count = string.Empty;
            string ppID = string.Empty;
            string ppname = await _currencyServer.InTheSwap(_ProductLend.PPName);   //获取供应商名称首字母转换
            var data = _currencyServer.ExecuteSqlAsync(null, @"SELECT top 1 Amount FROM ProductLend 
WHERE CONVERT(CHAR(8), CreateOn, 112) = CONVERT(CHAR(8), GETDATE(), 112) order by CreateOn desc");
            if (data.Rows.Count == 0) ppID = ppname + DateTime.Now.ToString("yyyymmdd") + "0001";
            foreach (DataRow item in data.Rows)
            {
                var Amounts = (Convert.ToInt32(item["Amount"].ToString().PadLeft(4, '0')) + 1).ToString().PadLeft(4, '0');
                ppID = ppname + DateTime.Now.ToString("yyyymmdd") + Amounts;
                count = Amounts;
            }
            await _dbContext._ProductLends.AddAsync(new AikeApplet_ProductLend
            {
                PPID = ppID,
                PPName = _ProductLend.PPName,
                PPCompany = _ProductLend.PPCompany,
                PPMan = _ProductLend.PPMan,
                PPTel = _ProductLend.PPTel,
                PPAddress = _ProductLend.PPAddress,
                PPFax = _ProductLend.PPFax,
                PPEmail = _ProductLend.PPEmail,
                PPUrl = _ProductLend.PPUrl,
                PPBank = _ProductLend.PPBank,
                PPGoods = _ProductLend.PPGoods,
                PPDesc = _ProductLend.PPDesc,
                CreateTime = new DateTime(),
                CreateOn = "秧俊",
                Amount = count
            });
            return await _dbContext.SaveChangesAsync() >= 0;
        }

        /// <summary>
        /// 删除供货商
        /// </summary>
        /// <param name="PPID">供货商id</param>
        /// <returns></returns>
        public async Task<bool> DeletedProductLendAsync(AikeApplet_ProductLend PPID)
        {
            if (PPID == null) throw new ArgumentNullException(nameof(PPID));

            _dbContext._ProductLends.Remove(PPID);

            return await _dbContext.SaveChangesAsync() >= 0;
        }

        /// <summary>
        /// 获取供货商信息
        /// </summary>
        /// <returns></returns>
        public async Task<IEnumerable<AikeApplet_ProductLend>> GetAllProductLendDataAsync()
        {
            return await _dbContext._ProductLends.AsQueryable().ToListAsync();
        }

        /// <summary>
        /// 通过供货商编号获取供货商信息
        /// </summary>
        /// <param name="PPID"></param>
        /// <returns></returns>
        public async Task<AikeApplet_ProductLend> GetProductLendDataByPPIDAsync(string PPID)
        {
            if (PPID == null) return null;
            return await _dbContext._ProductLends.FirstOrDefaultAsync(x => x.PPID == PPID);
        }

        /// <summary>
        /// 获取分页获取供货商信息
        /// </summary>
        /// <param name="_ProductLend"></param>
        /// <returns></returns>
        public async Task<DbQueryResult> GetProductLendPageListAsync(AikeApplet_ProductLend _ProductLend)
        {
            return await _currencyServer.PagingByRownumber("ProductLend", "PPID", _ProductLend.PageIndex, _ProductLend.PageSize
                , "CreateTime", 0, "", true, "*");
        }

        /// <summary>
        /// 修改供货商数据
        /// </summary>
        /// <param name="_ProductLend">供货商数据集</param>
        /// <returns></returns>
        public async Task<bool> UpdateProductLendAsync(AikeApplet_ProductLend _ProductLend)
        {
            if (_ProductLend.PPID == null) throw new ArgumentNullException(nameof(_ProductLend.PPID));
            var data = await GetProductLendDataByPPIDAsync(_ProductLend.PPID);
            if (data == null) throw new ArgumentNullException(nameof(data));
            data.PPID = _ProductLend.PPID;
            data.PPName = _ProductLend.PPName;
            data.PPCompany = _ProductLend.PPCompany;
            data.PPMan = _ProductLend.PPMan;
            data.PPTel = _ProductLend.PPTel;
            data.PPAddress = _ProductLend.PPAddress;
            data.PPFax = _ProductLend.PPFax;
            data.PPEmail = _ProductLend.PPEmail;
            data.PPUrl = _ProductLend.PPUrl;
            data.PPBank = _ProductLend.PPBank;
            data.PPGoods = _ProductLend.PPGoods;
            data.PPDesc = _ProductLend.PPDesc;
            data.ModeTime = DateTime.Now;
            data.ModeOn = _ProductLend.ModeOn;
            _dbContext._ProductLends.Update(data);
            return await _dbContext.SaveChangesAsync() >= 0;
        }
    }
}
